﻿using System.Collections.Generic;
using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 令叶在同级向下移动一个位置——不支持跨分支平移。
        /// </summary>
        public static void MoveLeafDown()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.LeafPanel == false)
            {
                MessageBox.Show("请先选中一个叶面板！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Expressions.LeafPanel curLeaf = curEx as Expressions.LeafPanel;
            if (curLeaf.LeafType == Expressions.LeafPanel.LeafPanelType.Root)
            {
                MessageBox.Show("树的叶首面板不支持平移操作！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (curLeaf.ParentExpression == null ||
                curLeaf.ParentExpression is Expressions.Tree == false) return;

            Expressions.Tree pTree = curLeaf.ParentExpression as Expressions.Tree;

            Expressions.LeafPanel preLevelLeaf = curLeaf.PreLevelLeaf;
            if (preLevelLeaf == null) return;

            List<Expressions.LeafPanel> sameLevelLeaves = preLevelLeaf.NextLevelLeaves;
            if (sameLevelLeaves == null || sameLevelLeaves.Count <= 0 ||
                sameLevelLeaves.Contains(curLeaf) == false) return;

            if (curLeaf.Index >= sameLevelLeaves.Count) return;

            Expressions.LeafPanel bottomLeaf = null;

            for (int i = curLeaf.Index + 1; i < sameLevelLeaves.Count; i++)
            {
                Expressions.LeafPanel lp = sameLevelLeaves[i];
                if (lp.LeafType == curLeaf.LeafType)
                {
                    bottomLeaf = lp;
                    //只要找到第一个索引大于（不一定是大１）当前叶、且方向与当前叶相同的即可。
                    break;
                }
            }

            if (bottomLeaf == null) return;

            int oldIndexOfBottomLeaf = bottomLeaf.Index;
            int oldIndexOfCurLeaf = curLeaf.Index;

            ModifingItem mi = new ModifingItem() { OldSelectedExID = curLeaf.ExID };

            //注意索引是交换而不是正好加、减１。
            Action actIndex1 = new Action(
                bottomLeaf.ExID, -1, bottomLeaf.GetType().Name, "Index",
                oldIndexOfBottomLeaf.ToString(), oldIndexOfCurLeaf.ToString());
            bottomLeaf.Index = oldIndexOfCurLeaf;

            mi.Add(actIndex1);

            Action actIndex2 = new Action(curLeaf.ExID, -1, curLeaf.GetType().Name,
                "Index", oldIndexOfCurLeaf.ToString(), oldIndexOfBottomLeaf.ToString());
            curLeaf.Index = oldIndexOfBottomLeaf;

            mi.Add(actIndex2);

            curLeaf.Editor.Do(mi);
            curLeaf.Editor.IsModified = true;

            pTree.RefreshLayout(true);
        }
    }
}
